package ioc

import (
	"fmt"
	"gitee.com/DonHz/basic-go/webook/pkg/ratelimit"
	"github.com/redis/go-redis/v9"
	"github.com/spf13/viper"
	"time"
)

func InitRedis() redis.Cmdable {
	type Config struct {
		Addr string `yaml:"addr"`
	}
	var cfg Config
	err:=viper.UnmarshalKey("redis", &cfg)
	if err!=nil {
		panic(any(err))
	}
	redisClient:=redis.NewClient(&redis.Options{
		//Addr: config.Config.Redis.Addr,
		//Addr: "webook-redis:6379",
		Addr: cfg.Addr,	// 读取配置文件
	})
	fmt.Println("redis address: ", cfg.Addr)
	return redisClient
}

func InitRateLimit(redisClient redis.Cmdable) ratelimit.Limiter {
	return ratelimit.NewRedisSlidingWindowLimiter(redisClient, time.Second, 100)
}
